/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package yoga.gui;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import javax.swing.JPanel;

/**
 *
 * @author Yogev Montekyo
 */
public class GraphicsUtils
{

    private static Color colorEdges = Color.BLUE;
    private static Color colorVertices = Color.RED;
    private static Color colorForeground = Color.BLACK;
    private static Color colorBackground = Color.GRAY;
    private static final int vertixDiameter = 4;

    public static Color getColorEdges()
    {
        return colorEdges;
    }

    public static Color getColorVertices()
    {
        return colorVertices;
    }

    public static Color getColorForeground()
    {
        return colorForeground;
    }

    public static Color getColorBackground()
    {
        return colorBackground;
    }

    public static void setColorEdges(Color colorEdges)
    {
        GraphicsUtils.colorEdges = colorEdges;
    }

    public static void setColorVertices(Color colorVertices)
    {
        GraphicsUtils.colorVertices = colorVertices;
    }

    public static void setColorForeground(Color colorForeground)
    {
        GraphicsUtils.colorForeground = colorForeground;
    }

    public static void setColorBackground(Color colorBackground)
    {
        GraphicsUtils.colorBackground = colorBackground;
    }

    public enum PaintingModes
    {

        EdgesAndVertices,
        EdgesOnly,
        VerticesOnly;
    }

    public static void paintPolygon(Graphics graphics, Polygon polygon, PaintingModes paintMode)
    {
        switch (paintMode) {
            case EdgesAndVertices:
                drawPolygon(graphics, polygon);
                drawVertices(graphics, polygon);
                break;
            case EdgesOnly:
                drawPolygon(graphics, polygon);
                break;
            case VerticesOnly:
                drawVertices(graphics, polygon);
                break;
            default:
                drawPolygon(graphics, polygon);
                drawVertices(graphics, polygon);
        }
    }

    private static void drawPolygon(Graphics graphics, Polygon polygon)
    {
        //Draw polygon edges        
        graphics.setColor(colorEdges);
        graphics.drawPolygon(polygon);
    }

    private static void drawVertices(Graphics graphics, Polygon polygon)
    {
        //Draw polygon vertices
        graphics.setColor(colorVertices);
        for (int i = 0; i < polygon.npoints; i++) {
            int x = polygon.xpoints[i] - (vertixDiameter / 2);
            int y = polygon.ypoints[i] - (vertixDiameter / 2);
            
            graphics.fillOval(x, y, vertixDiameter, vertixDiameter);
            //graphics.drawString(""+i , x, y);
        }
    }
    
    public static void saveGraphicsToJpeg(JPanel drawPanel, File file)
    {
        Dimension size = drawPanel.getSize();                
        BufferedImage myImage =
                new BufferedImage(size.width, size.height,
                BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = myImage.createGraphics();
        drawPanel.paint(g2);
        try {
            OutputStream out = new FileOutputStream(file + ".jpg");
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
            encoder.encode(myImage);
            out.close();
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}
